﻿using BingWallpaper.Common;
using BingWallpaper.Models;
using BingWallpaper.Models.Entitys;
using BingWallpaper.Services;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BingWallpaper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public ObservableCollection<BingImagesModel> ImageList = new ObservableCollection<BingImagesModel>();

        public MainWindow()
        {
            InitializeComponent();

            //开机自启
            AutoStart.OnStart();

            BingWallpaperBLL bingWallpaperBLL = new BingWallpaperBLL();



            for (int i = 0; i < 10; i++)
            {
                var bingData = BingHelper.DownloadJson(i);
                //  ImageList.Add(new BingImagesModel { Url = "https://www.bing.com" + bingData.images[0].url, Title = bingData.images[0].title });
                bingWallpaperBLL.Save(new BingImagesModel { Url = "https://www.bing.com" + bingData.images[0].url, Title = bingData.images[0].title, FullStartDate = bingData.images[0].fullstartdate, StartDate = bingData.images[0].startdate, Copyright = bingData.images[0].copyright });
            }
            var list = bingWallpaperBLL.GetList();
            //foreach (var item in collection)
            //{

            //}
            //ImageList.Insert(0,);

            this.listBox1.ItemsSource = list;
        }

        private void SetBackground_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
            BingImagesModel patientInfo = btn.DataContext as BingImagesModel;


            WebResponse response = null;
            Stream stream = null;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(patientInfo.Url);
            response = request.GetResponse();
            stream = response.GetResponseStream();

            using (Bitmap bgimage = new Bitmap(stream))
            {
                bgimage.Save("bg.jpg");
            }

            using (var image = System.DrawingCore.Image.FromFile("bg.jpg"))
            {
                image.Save("bg.bmp", ImageFormat.Bmp);
                SystemParametersInfo(20, 0, AppDomain.CurrentDomain.BaseDirectory + "bg.bmp", 1 | 2);
            }


        }

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(
                 int uAction,
                 int uParam,
                 string lpvParam,
                 int fuWinIni
                 );
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
            BingImagesModel patientInfo = btn.DataContext as BingImagesModel;

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Image Files (*.jpg, *.bmp, *.png)|*.jpg;*.bmp;*.png | All Files | *.*";
            sfd.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
            if (sfd.ShowDialog() == true)
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(patientInfo.ImageBit));
                //if (string.IsNullOrWhiteSpace(sfd.FileName))
                //{
                //    sfd.FileName = patientInfo.Title;
                //}
                using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
                    encoder.Save(stream);
            }


        }


    }
}
